package anc.tankshooter.controls;

import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;

import anc.tankshooter.ui.ResourcesManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.text.InputType;
import android.view.Gravity;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class InputText extends ButtonSprite implements OnClickListener {

	
	private final String	mTitle;
	private final Text		mText;
	
	private boolean mIsEnable;
	private boolean			mIsPassword;
	private String			mValue;
	private final int       mMaxChar;

	public InputText(float pX, float pY, final String title, ITextureRegion mTextBoxTexture,
			Font font,int max) {
		super(pX, pY, mTextBoxTexture, ResourcesManager.getInstance().mVertexManager,null);
		mValue="";
		mIsEnable = true;
		this.mMaxChar = max;
		this.mTitle = title;
		this.mText = new Text(5, 0, font, "", max, ResourcesManager.getInstance().mVertexManager);
		mText.setX(5); mText.setY(getHeight()/2 - mText.getHeight()/2);
		attachChild(this.mText);
		setOnClickListener(this);
	}

	public String getText() {
		return this.mValue.trim();
	}

	public boolean isPassword() {
		return this.mIsPassword;
	}
	public void setEnable(boolean isEnable){
		mIsEnable = isEnable;
	}
	public boolean isEnable(){
		return this.mIsEnable;
	}

	@Override
	public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		if(mIsEnable)
			showTextInput();
	}

	public void setPassword(final boolean isPassword) {
		this.mIsPassword = isPassword;
	}

	public void setText(String text) {
		this.mValue = text;

		if (isPassword() && text.length() > 0)
			text = String.format("%0" + text.length() + "d", 0).replace("0", "*");

		this.mText.setText(text);
	}

	public void showTextInput() {
		final Context context = ResourcesManager.getInstance().mActivity;
		
		 ResourcesManager.getInstance().mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				final AlertDialog.Builder alert = new AlertDialog.Builder(context);

				alert.setTitle(InputText.this.mTitle);
				final EditText editText = new EditText(context);
				editText.setTextSize(20f);
				editText.setText(InputText.this.mValue);
				editText.setGravity(Gravity.CENTER_HORIZONTAL);
				if (isPassword())
					editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

				alert.setView(editText);

				alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						String text = editText.getText().toString().replaceAll("[^0-9_A-Za-z]","");
						
						if(mMaxChar>0){
							if(text.length()>mMaxChar)
								text = text.substring(0,mMaxChar-1);
							
							setText(text);
						}
					}
				});

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {

					}
				});

				final AlertDialog dialog = alert.create();
				dialog.setOnShowListener(new OnShowListener() {
					@Override
					public void onShow(DialogInterface dialog) {
						editText.requestFocus();
						final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
					}
				});
				dialog.show();
			}
		});
	}

}
